<html>
<head>
    <title>Title</title>
	<script type="text/javascript" src="jquery.min.js"></script>
</head>
<body>
    
        邮箱：<input type="text" name="email">
        密码：<input type="password" name="password">
		确认密码：<input type="password1" name="password1">
		验证码：<input type="text" name="code"> <input type="button" id="btn_code" value="获取验证码" onclick="createCode()">
        <input type="button" value="注册" onclick="register()">
    
</body>

<script>

var lastTime;

function createCode(){

	var nowTime = new Date().getTime();
	
	if(lastTime){
		if(lastTime+5000 > nowTime){
			alert("1分钟内只允许获取1次验证码");
			return false;
		}
	}else{
		lastTime=nowTime;
	}
	
	var email = $("input[name='email']").val();
	$.ajax({
		url: 'http://localhost/api/code/create/'+email,
		type: 'post',
		contentType: 'application/json;charset=utf-8',
		dataType: 'json',
		success: function (data) {
			console.log(data);
			if(data){
				alert("验证码已发送");
			}
			lastTime=nowTime;
		}
	})
}

function register(){

	var email = $("input[name='email']").val();
	var password = $("input[name='password']").val();
	var password1 = $("input[name='password1']").val();
	var code = $("input[name='code']").val();
	
	if(password != password1){
		alert("两次密码不一致");
		return false;
	}
	
	$.ajax({
		url: 'http://localhost/api/user/register/'+email+'/'+password+'/'+code,
		type: 'post',
		contentType: 'application/json;charset=utf-8',
		dataType: 'json',
		success: function (data) {
			console.log(data);
			if(data.status){
				window.location.href = "http://localhost/static/welcome.html";
			}else{
				alert(data.msg);
			}
		}
	})
}


</script>

</html>